import {proxy} from "@@/exports";
import {getMarketSetting, SettingVO} from "@/services/SettingService";
import {getNotifyList, NotifyVO} from "@/services/NotifyService";
import {CustomerChannelVO, getCustomerChannelList} from "@/services/CustomerChannelService";

interface SettingModelType {
    setting?: SettingVO;
    updateModalState: boolean;
    settingActiveTabKey: string;
    notifyChannelList: NotifyVO[];
    customerChannelList: CustomerChannelVO[]

}

const state = proxy<SettingModelType>({
    setting: undefined,
    updateModalState: false,
    settingActiveTabKey: 'default',
    notifyChannelList: [],
    customerChannelList: []
})

const actions = {
    /**
     * 设置修改模态框状态
     * @param modalState 状态
     */
    setUpdateModalState(modalState: boolean) {
        state.updateModalState = modalState
    },
    /**
     * 打开模态框
     * @param modalState
     * @param setting
     */
    openUpdateModal(modalState: boolean, setting?: SettingVO) {
        if (setting) {
            setting.customerNotify = JSON.parse(setting?.customerNotify);
            setting.orderNotify = JSON.parse(setting?.orderNotify)
        }
        state.setting = setting
        state.updateModalState = modalState
    },


    initDefaultSetting() {
        getMarketSetting().then(r => state.setting = {
            ...r,
            customerNotify: JSON.parse(r.customerNotify),
            orderNotify: JSON.parse(r.orderNotify)
        });
        getNotifyList().then(r => state.notifyChannelList = r)
    },
    initChannelSetting() {
        getCustomerChannelList().then(r => state.customerChannelList = r)
    },

    setSettingActiveTabKey(tabKey: string) {
        switch (tabKey) {
            case 'default':
                actions.initDefaultSetting();
                break
            case 'channelSetting':
                actions.initChannelSetting()
                break
        }
        state.settingActiveTabKey = tabKey
    },
};


export const SettingModel = {state, actions}